package cn.ylz.ylmall.account.controller;

import cn.ylz.ylmall.account.pojo.param.PermissionAddnewParam;
import cn.ylz.ylmall.account.pojo.param.PermissionUpdateParam;
import cn.ylz.ylmall.account.pojo.vo.PageData;
import cn.ylz.ylmall.account.pojo.vo.PermissionListVO;
import cn.ylz.ylmall.account.pojo.vo.PermissionStandardVO;
import cn.ylz.ylmall.account.service.IPermissionService;
import cn.ylz.ylmall.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/v1/users/permission")
@Api(tags = "1.2. 账号管理-权限管理")
@Slf4j
@Validated
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @PostMapping("add-new")
    @ApiOperation("新增权限")
    @PreAuthorize("hasAuthority('/account/permission/add-new')")
    @ApiOperationSupport(order = 1)
    public JsonResult addNew(@Valid PermissionAddnewParam addnewParam) {
        permissionService.addnew(addnewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除权限")
    @PreAuthorize("hasAuthority('/account/permission/delete')")
    @ApiOperationSupport(order = 2)
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的权限ID值！") Long id){
        permissionService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/account/permission/update')")
    @ApiOperation("修改权限")
    @ApiOperationSupport(order = 3)
    public JsonResult update(@PathVariable @Range(min = 1, message = "请提交有效的权限ID值！") Long id,@Valid PermissionUpdateParam param){
        permissionService.update(id,param);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询权限")
    @PreAuthorize("hasAuthority('/account/permission/read')")
    @ApiOperationSupport(order = 4)
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "请提交有效的权限ID值！") Long id){
        PermissionStandardVO standardVO = permissionService.getPermissionStandard(id);
        return JsonResult.ok(standardVO);
    }

    @GetMapping("")
    @ApiOperation("查询权限列表")
    @PreAuthorize("hasAuthority('/account/permission/read')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult list(Integer page){
        Integer pageNum = page == null ? 1 : page;
        PageData<PermissionListVO> list = permissionService.list(pageNum);
        return JsonResult.ok(list);
    }

}
